﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Linq;
using System.Linq;

namespace ProyectoEscuela
{
    public partial class frmBuscarPersona : ProyectoEscuela.frmBaseForm
    {

        String myTipoPersona;

        Form myCaller;
        Persona myPersona = new Persona();

        dbProyectoEscuelaDataContext dbPE = Global.getInstance().db;

       
        public frmBuscarPersona(String tipoPersona)
        {
            InitializeComponent();
            myTipoPersona = tipoPersona;

        }
        public frmBuscarPersona()
        {
            InitializeComponent();
            myTipoPersona = "persona";

        }

        public frmBuscarPersona(Form caller, String tipoPersona)
        {
            // Por si se llamaa a este formulario de forma no modal

            InitializeComponent();
            this.myCaller = caller;
            myTipoPersona = tipoPersona;
        }

        public void setCaller(Form caller)
        {
            myCaller = caller;
        }

        public Form getCaller()
        {
            return this.myCaller;
        }

        public Persona getPersona()
        {
            return myPersona;
        }

        public Agente getAgente()
        {
            Agente myAgente = new Agente();

            return myAgente.getAgente(myPersona.TipoDoc, myPersona.Numdoc);


        }

        public Alumno getAlumno()
        {
            Alumno myAlumno = new Alumno();

            return myAlumno.getAlumno(myPersona.TipoDoc, myPersona.Numdoc);


        }

        public string getTipoDoc()
        {
            return myPersona.TipoDoc;

        }
        public string getNumDoc()
        {
            return myPersona.Numdoc;

        }

        private void frmBuscarPersona_Load(object sender, EventArgs e)
        {
           
            //dgPersonas.DataSource = from pers in dbPE.Personas select pers;
            if (myTipoPersona == "agente")
            {
                this.dgBuscarPersonas.DataSource = from pers in dbPE.Personas where pers is Agente select pers;
            }
            else
            {
                this.dgBuscarPersonas.DataSource = from pers in dbPE.Personas where pers is Alumno select pers;

            }

          
        }

        private void btnBuscar_Click(object sender, EventArgs e)
        {

            var results = from pers in dbPE.Personas select pers;

            if (myTipoPersona == "agente")
            {
               results = (from pers in results where pers is Agente select pers);
             }else{
                 results = (from pers in results where pers is Alumno select pers);
             }

            if(txbApellido.Text.Trim().Length != 0){
                results = from pers in results where pers.Apellido.StartsWith(txbApellido.Text) select pers;

            }

            if (txtNombre.Text.Trim().Length != 0){
                results = from pers in results where pers.Nombre.StartsWith(txtNombre.Text) select pers;
            }

            if (tbxDni.Text.Trim().Length != 0){
                results = from pers in results where pers.Numdoc == this.tbxDni.Text select pers;

            }
            this.dgBuscarPersonas.DataSource = results;
                    
            }
 
        private void cbxTipoPersona_SelectionChangeCommitted(object sender, EventArgs e)
        {
            dgBuscarPersonas.DataSource = from pers in dbPE.Personas where pers.TipoPersona == cbxTipoPersona.Text select pers;
        }

        private void btnLimpiar_Click(object sender, EventArgs e)
        {
            dgBuscarPersonas.DataSource = null;
            dgBuscarPersonas.Refresh();
        }

        private void brnNuevaPersona_Click(object sender, EventArgs e)
        {
            
        }

        private void dgPersonas_CellDoubleClick(object sender, DataGridViewCellEventArgs e) 
        {

           
           
           
            /*
            if( dgvPersonas.Rows[e.RowIndex].Cells[17].Value.ToString()=="Alumno")
                {
                  //  frmABMAlumno formABMalumno= frmABMAlumno.GetInstance();
                  //  formABMalumno.MdiParent = MdiParent;
                  //  formABMalumno.Show();
                }
            else
            {
               // frmABMAgente formABMAgente= frmABMAgente.GetInstance();
               // formABMAgente.MdiParent = MdiParent;
               // formABMAgente.Show();
            }
             * */
        }

       

        private void dgPersonas_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
        {
          //  System.Windows.Forms.MessageBox.Show("Hola mundo");
        }

     
        

        private void cbxTipoPersona_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

      

        private void Georgina(object sender, EventArgs e)
        {
            
        }

        private void Evento(object sender, System.EventArgs e)
        {
          
            String tipoDoc, numDoc;

            tipoDoc = dgBuscarPersonas.Rows[dgBuscarPersonas.SelectedRows[0].Index].Cells["TipoDoc"].Value.ToString();
            numDoc = dgBuscarPersonas.Rows[dgBuscarPersonas.SelectedRows[0].Index].Cells["Numdoc"].Value.ToString();

            myPersona = myPersona.getPersona(tipoDoc, numDoc);

            if (this.myCaller != null)
            {
                // por si este formulario no fue llamado de forma modal
                this.myCaller.Show();

            }

            this.Hide();
        }

        private void dgBuscarPersonas_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dgBuscarPersonas_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
        {

        }



    }
}